<!-- 2020年2月15日14:57:57 完成 -->
<template>
	<view>
		<view class="edgeInsetTop"></view>
		<!-- <wanl-empty v-if="addressList.length === 0 && status !== 'loading'" text="还没添加任何地址" />/ -->
		<view class="margin-top-bj">
			<view class="cu-list menu-avatar">
				<uni-swipe-action ref="swipeAction">
					<uni-swipe-action-item v-for="(item, index) in addressList" :right-options="item.select.options"
						:key="item.id" @click="swipeClick($event, index,item,'edit')" @tap="checkAddress(item)">
						<!-- <view class="cu-item flex" :class="modalName == 'move-box-' + index ? 'move-cur' : ''"
							v-if="addressList" v-for="(item, index) in addressList" :key="index"
							@tap="checkAddress(item)" @touchstart="ListTouchStart" @touchmove="ListTouchMove"
							@touchend="ListTouchEnd" :data-target="'move-box-' + index"> -->
						<view class="list flex">
							<view class="cu-avatar round lg" style="background-color: #EBBE87;">{{ item.name }}
							</view>
							<view class="content" style="margin-left: 36upx;">
								<view style="margin-top: 44upx;">
									<text class="wanl-pip margin-right-sm text-lg"
										style="margin-right: 20upx;">{{ item.name }}</text>
									<text class="wanl-gray-light text-sm">{{ item.mobile }}</text>
								</view>
								<view class="text-sm wanl-pip text-cut-2" style="line-height: 1.3; width: 590upx;">
									<text class="wlIcon-dizhi text-red margin-right-xs" v-if="item.default == 1"></text>
									{{ item.province }} {{ item.city }} {{ item.district }} {{ item.address }}
								</view>
							</view>
							<view class="action" @tap.stop="addAddress('edit', item)"><text
									class="wlIcon-xiugaioryijian text-xl"></text></view>
							<!-- <view class="move">
									<view class="bg-grey" @tap.stop="addAddress('edit', item)">编辑</view>
									<view class="bg-red" @tap.stop="delAddress(item.id, index)">删除</view>
								</view> -->
						</view>
						<!-- </view> -->
					</uni-swipe-action-item>
				</uni-swipe-action>
				<wanl-empty text="没有找到任何地址" src="address_default3x" />
			</view>
		</view>
		<!-- <uni-load-more :status="status" :content-text="contentText" /> -->
		<view class="edgeInsetBottom"></view>
		<view class="wanlian cu-bar tabbar foot flex flex-direction"><button @tap="addAddress('add')"
				class="cu-btn wanl-bg-orange lg">添加地址</button></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				source: 0,
				order_id: 0,
				addressList: [],
				modalName: null,
				listTouchStart: 0,
				listTouchDirection: null,
				//上拉下拉
				total: 0, //数据量
				current_page: 1, //当前页码
				last_page: 1, //总页码
				status: 'loading',
				contentText: {
					contentdown: ' ',
					contentrefresh: '正在加载..',
					contentnomore: ''
				}
			};
		},
		// 监听下拉刷新
		onPullDownRefresh() {
			this.current_page = 1;
			this.reload = true;
			this.loadData();
		},
		// 监听上拉加载
		onReachBottom() {
			//判断是否最后一页
			if (this.current_page >= this.last_page) {
				this.status = 'noMore';
			} else {
				this.current_page = this.current_page + 1; //页码+1
				this.status = 'loading';
				this.loadData(true);
			}
		},
		onLoad(option) {
			this.source = option.source;
			this.order_id = option.order_id;
			this.loadData();
		},
		methods: {
			// 滑动删除/编辑
			swipeClick(e, index, item, type) {
				let {
					content
				} = e;
				if (content.text === '删除') {
					uni.showModal({
						title: '提示',
						content: '是否删除',
						success: res => {
							if (res.confirm) {
								this.addressList.splice(index, 1);
								this.$request({
									url: 'address/deladdress',
									method: 'POST',
									data: {
										id: item.id
									}
								}).then(res => {
								})
							} else if (res.cancel) {
							}
						}
					});
				} else {
					let name = JSON.stringify(item)
					uni.navigateTo({
						url: './addressManage?type=' + type + '&data=' + name
					})
				}
			},
			// 获取地址列表
			loadData() {
				this.$request({
					url: 'address/getaddress',
				}).then(res => {
					res.data.data.data.map((item, index) => {
						const obj = {
							id: index,
							options: [{
									text: '编辑'
								},
								{
									text: '删除',
									style: {
										backgroundColor: 'rgb(255,58,49)'
									}
								}
							],
						}
						this.$set(item, 'select', obj)
					})
					this.addressList = res.data.data.data
				})
			},
			//添加或修改成功之后回调
			refreshList(data, type) {
				this.$request({
					url: '	address/address',
					method: 'POST',
					data: {
						data: data,
						type: type
					},
				}).then(res => {
					this.loadData();
				})
			},
			//选择地址
			checkAddress(item) {
				if (this.source == 1) {
					this.prePage().loadData(item.id);
					uni.navigateBack({})
				} else if (this.source == 2) { //我的订单修改地址  地址ID   返回订单ID
					this.prePage().refreshList(item.id, this.order_id);
					uni.navigateBack({})
				} else if (this.source == 3) {
					this.prePage().refreshList(item)
					uni.navigateBack({})
				}
			},
			// 新增地址
			addAddress(type, item) {
				let name = JSON.stringify(item)
				uni.navigateTo({
					url: './addressManage?type=' + type + '&data=' + name
				})
			}
		}
	};
</script>

<style>
	.cu-avatar.lg {
		width: 90rpx;
		height: 90rpx;
		text-align: center;
		line-height: 90upx;
		border-radius: 50%;
		font-size: 1.5em;
		color: #fff;
		margin-left: 20rpx;
		margin-top: 44rpx;
	}

	.cu-list.menu-avatar>.cu-item {
		height: 160rpx;
	}

	.wanlian.cu-bar.tabbar {
		background-color: transparent;
	}

	.wanlian.cu-bar.tabbar .cu-btn {
		width: 686upx;
		height: 86upx;
		background: linear-gradient(180deg, #283145 0%, #122041 100%);
		border-radius: 52upx;
		color: #fff;
		position: absolute;
		bottom: 74upx;
		left: 32upx;
	}

	.wanlian.cu-bar.tabbar .cu-btn.lg {
		font-size: 32rpx;
		height: 86rpx;
	}

	.list {
		height: 172upx;
	}
</style>
